package com.tongtong.config.datasource;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;
import com.tt.datasource.CustomSqlInjector;
import com.tt.mybatis.MetaObjectHandlerConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.SneakyThrows;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration(value = "MybatisConfig")
public class MyBatisConfig {
    @Autowired
    private MetaObjectHandlerConfig metaObjectHandler;
    @Primary
    @SneakyThrows
    @Bean(name="dataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mumuda")
    public DataSource dataSourceChat() {
        return new HikariDataSource();
    }
    @Bean(name="sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryChat(@Qualifier("dataSource") DataSource dataSource,MetaObjectHandlerConfig metaObjectHandlerConfig) {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //分页插件
        PageInterceptor iterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "false");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        iterceptor.setProperties(properties);
        //添加插件
        bean.setPlugins(new Interceptor[]{iterceptor});
        //增加批量插入
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setSqlInjector(new CustomSqlInjector());
        globalConfig.setMetaObjectHandler(metaObjectHandlerConfig);
        bean.setGlobalConfig(globalConfig);

        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
//            bean.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml"));
            bean.setConfigLocation(resolver.getResource("classpath:config/configuration.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManagerChat(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name="sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplateChat(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurerChat() {
        MapperScannerConfigurer config= new MapperScannerConfigurer();
        config.setSqlSessionFactoryBeanName("sqlSessionFactory");
        config.setBasePackage("com.tongtong");
        return config;
    }

}
